package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.impl.AlbumInfoDegradeFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 专辑购买进入到结算页所对应的策略实现类
 */
@Service
@Slf4j
public class AlbumOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient ;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient ;

    @Autowired
    private RedisTemplate<String , String> redisTemplate;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 为了增强代码的健壮性，可以添加当前登录用户是否购买过该专辑的逻辑判断
        Long itemId = tradeVo.getItemId();
        Result<Boolean> paidAlbumResult = userInfoFeignClient.isPaidAlbum(itemId);
        Boolean ifAbsent = paidAlbumResult.getData();
        if(ifAbsent) {
            throw new GuiguException(ResultCodeEnum.NO_REPEAT_BUY) ;
        }

        // 创建一个OrderInfoVo对象，封装结算页所需要的数据
        OrderInfoVo orderInfoVo =  new OrderInfoVo() ;

        // 设置的相关的金额
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(itemId);
        AlbumInfo albumInfo = albumInfoResult.getData();
        BigDecimal originalAmount = albumInfo.getPrice();
        orderInfoVo.setOriginalAmount(originalAmount);

        // 获取当前登录用户信息
        Long userId = AuthContextHolder.getUserId();
        Result<UserInfo> userInfoResult = userInfoFeignClient.getUserInfoById(userId);
        UserInfo userInfo = userInfoResult.getData();
        Integer isVip = userInfo.getIsVip();
        if(isVip.intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {        // 该用户是vip并且vip没有过期

            // 获取vip的折扣
            BigDecimal vipDiscount = albumInfo.getVipDiscount();
            if(vipDiscount.intValue() == -1) {      // 没有折扣
                orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
                orderInfoVo.setOrderAmount(originalAmount);
            }else {
                BigDecimal orderAmount = originalAmount.multiply(vipDiscount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
                BigDecimal derateAmount = originalAmount.subtract(orderAmount);
                orderInfoVo.setDerateAmount(derateAmount);
                orderInfoVo.setOrderAmount(orderAmount);
            }

        }else {

            // 获取vip的折扣
            BigDecimal discount = albumInfo.getDiscount() ;
            if(discount.intValue() == -1) {  // 没有折扣
                orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
                orderInfoVo.setOrderAmount(originalAmount);
            }else {
                BigDecimal orderAmount = originalAmount.multiply(discount)
                        .multiply(new BigDecimal("10"))
                        .divide(new BigDecimal("10"), 2, RoundingMode.HALF_UP);
                BigDecimal derateAmount = originalAmount.subtract(orderAmount);
                orderInfoVo.setDerateAmount(derateAmount);
                orderInfoVo.setOrderAmount(orderAmount);
            }

        }

        // 订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo() ;
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(orderInfoVo.getOrderAmount());
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        // 减免明细列表
        BigDecimal derateAmount = orderInfoVo.getDerateAmount();
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>() ;
        if(derateAmount.doubleValue() > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo() ;
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("专辑购买折扣");
            orderDerateVoList.add(orderDerateVo) ;
        }
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        // 时间戳
        this.setOtherData(orderInfoVo , tradeVo , redisTemplate);

        // 返回
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;        // 1001
    }

}
